//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    avatarUrl: "",
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    selectIndex:0,
    imageList: [],
    categoryList:[
      {
        "title": "花瓣",
        "imageList": ["../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png"]
      },
      {
        "title": "变圆",
        "imageList": ["../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png"]
      },
      {
        "title": "挂件",
        "imageList": ["../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png"]
      },
      {
        "title": "加V",
        "imageList": ["../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png","../image/img3.png"]
      },
      {
        "title": "状态",
        "imageList": ["../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png", "../image/img0.png"]
      },
      {
        "title": "未读数",
        "imageList": ["../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png", "../image/img1.png"]
      },
      {
        "title": "明星",
        "imageList": ["../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png", "../image/img2.png"]
      },
      {
        "title": "大学",
        "imageList": ["../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png", "../image/img3.png"]
      },
      {
        "title": "动漫",
        "imageList": ["../image/img0.png", "../image/img1.png", "../image/img2.png", "../image/img3.png", "../image/img0.png", "../image/img1.png", "../image/img2.png", "../image/img3.png"]
      },
      {
        "title": "游戏",
        "imageList": ["../image/img0.png", "../image/img1.png", "../image/img2.png", "../image/img3.png", "../image/img0.png", "../image/img1.png", "../image/img2.png", "../image/img3.png"]
      }
    ]
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    this.setData({
      imageList:this.data.categoryList[0].imageList
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        avatarUrl: app.globalData.userInfo.avatarUrl,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          avatarUrl: res.userInfo.avatarUrl,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            avatarUrl: res.userInfo.avatarUrl,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      avatarUrl: e.detail.userInfo.avatarUrl,
      hasUserInfo: true
    })
  },
  // 选择某一组

  categoryItemClick: function(e){
    let index = parseInt(e.currentTarget.id);
    this.setData({
      selectIndex: e.currentTarget.id,
      imageList: this.data.categoryList[index].imageList
    })
  },

  // 选择图片
  selectIcon: function(){

  },
  btnAction: function(e){
    if(e.currentTarget.id == "selectBtn"){// 选择图片
      this.chooseImage();
    }else if(e.currentTarget.id == "saveBtn"){// 保存图片
      this.saveImage();
    }else if(e.currentTarget.id == "shareLabel"){// 分享朋友

    }
  },
  // 选择图片
  chooseImage() {
    const that = this
    wx.chooseImage({
      success: res => {
        this.setData({
          avatarUrl: res.tempFilePaths[0]
        })
      }
    })
  },
  // 预览图片
  previewIcon(){
    wx.previewImage({
      current: '',
      urls: [],
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 保存图片
  saveImage(){
    let that = this;
    wx.getSetting({
      success(res){
        if (!res.authSetting['scope.writePhotosAlbum']){// 未授权，先申请权限
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success(re){
              that.saveToAlbum();
            }
          })
        }else{// 已经授权，直接保存到相册
          that.saveToAlbum();
        }
      }  
    })
  },
  // 保存到相册
  saveToAlbum(){
    console.log(this.data.avatarUrl);
    if(this.data.avatarUrl.indexOf("http") != -1 ){// 网络图片
      this.saveNetImage();
    }else{// 本地图片
      this.saveLocalImage();
    }
  },
  // 保存本地图片
  saveLocalImage(){
    let imgUrl = this.data.avatarUrl;
    wx.getImageInfo({
      src: imgUrl,
      success: function (ret) {
        var path = ret.path;
        wx.saveImageToPhotosAlbum({
          filePath: path,
          success(result) {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
          },
        })
      }
    })
  },
  // 保存网络图片
  saveNetImage(){
    wx.downloadFile({
      url: this.data.avatarUrl,
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(result) {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
          }
        })
      }
    })
  },
  
  // 分享图片
  shareImage(){

  }
})
